import os, sys
home = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append('/home/lanwenhong/tianya/trunk/client/python')
from memlinkclient import *
import test_init
import urllib
import urllib2
import json
import time

class BaseOp:
    def __init__(self, memlink_path, memlink_addr, rport, wport, dserver, dport, htdocs_path):
        self.tinit = test_init.TestInit(memlink_path, memlink_addr, wport, rport, dserver, dport, htdocs_path)
        self.urlbase = 'http://' + self.tinit.dserver + ':' + str(self.tinit.dport) + '/'
        print self.urlbase

    def create_table(self, table_name, itemcount):
        store = 'hbase'
        fields = ''
        for i in xrange(0, itemcount):
            if i == itemcount - 1:
                fields += 'item%d' % i
            else:
                fields += 'item%d,' % i
            
        rowkey = 'item0'
        desc = 'testing'
        address = self.tinit.memlink_addr

        params = 'store=%s&field=%s&rowkey=%s&desc=%s&address=%s' % (store, fields, rowkey,  desc, address)
        #print params
        url = self.urlbase + table_name + '/' + 'create' + '?%s'  % params
        print url
        f = urllib.urlopen(url)
        result = print f.read()

        if '200' in result:
            return 0
        return -1

    def create_test_data(self, table_name, itemcount):
        url = self.urlbase + table_name + '/' + 'add' 
        jdic = {}
        jlist = []
        line = {}

        for i in xrange(0, 1000):
            line = {}
            for j in xrange(0, itemcount):
                if j == 0:
                    key = 'item%d' % j
                    line[key] = '%d' % i
                else:
                    key = 'item%d' % j
                    line[key] = '%d' %  3
            
            jlist.append(line)
        
        jdic['array'] = jlist
        
        jstr = json.dumps(jdic)
        
        print url
        fd = urllib.urlopen(url, jstr)
        result = print fd.read()
        
        if '200' in result:
            return 0, jstr

        return -1, None

    def create_index(self, table_name, item, prefix):
        params = 'key=%s&prefix=%s' % (item, prefix)
        print params
        url = self.urlbase + table_name + '/' +  'index_add' + '?%s' % params
        print url
        f = urllib.urlopen(url)
        print f.read()

        if '200' in result:
            return 0 
        return  -1 

    def rebuild_index(self, table_name, item):
        params = 'indexkey=%s' % (item)
        print params
        url = self.urlbase + table_name + '/' + 'index_rebuild' + '?%s' % params
        print url
        f = urllib.urlopen(url)
        print f.read()

        if '200' in result:
            return 0
        return -1
    
    def remove_index(self, table_name, item):
        params = 'indexkey=%s' % (item)
        print params
        url = self.urlbase + table_name + '/' + 'index_remove' + '?%s' % params
        print url
        f = urllib.urlopen(url)
        print f.read()

        if '200' in result:
            return 0
        return -1

    def test_clean(self, table_name, htdocs_path, memlink_path):
        url = self.urlbase + table_name + '/' + 'remove'
        print url
        f = urllib.urlopen(url)
        print f.read()

        url = self.urlbase + 'storage' + '/' + 'remove'
        print url
        f = urllib.urlopen(url)
        print f.read()

        url = self.urlbase + 'index' + '/' + 'remove'
        print url
        f = urllib.urlopen(url)
        print f.read()

        time.sleep(3)

        cmd = 'killall dataserver' 
        os.system(cmd)
        time.sleep(2)

        cmd = 'killall -9 memlink'
        os.system(cmd)
        time.sleep(2)

        cmd = 'rm ' + memlink_path + '/' + 'data/*'
        os.system(cmd)

